package com.cn.shuangzi.userinfo.activity;

import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.aspsine.irecyclerview.IRecyclerView;
import com.aspsine.irecyclerview.OnRefreshListener;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.cn.shuangzi.R;
import com.cn.shuangzi.SZBaseActivity;
import com.cn.shuangzi.activity.SZInterfaceActivity;
import com.cn.shuangzi.bean.ActivelyPaymentInfo;
import com.cn.shuangzi.retrofit.SZCommonResponseListener;
import com.cn.shuangzi.retrofit.SZRetrofitManager;
import com.cn.shuangzi.userinfo.adp.ContinuePayManageAdp;
import com.cn.shuangzi.userinfo.common.UserManager;
import com.cn.shuangzi.util.SZIRecyclerViewUtil;
import com.cn.shuangzi.util.SZToast;
import com.cn.shuangzi.util.SZUtil;
import com.cn.shuangzi.view.sweet_alert.SweetAlertDialog;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.List;

import androidx.annotation.NonNull;

/**
 * Created by CN.
 */
public abstract class SZContinuePayManageActivity extends SZBaseActivity implements SZInterfaceActivity {
    private IRecyclerView recyclerView;
    private ContinuePayManageAdp continuePayManageAdp;
    private List<ActivelyPaymentInfo> continuePayInfoList;
    @Override
    protected int onGetChildView() {
        return R.layout.activity_continue_pay_manage;
    }

    @Override
    protected void onBindChildViews() {
        recyclerView = findViewById(R.id.recyclerView);
    }

    @Override
    protected void onBindChildListeners() {
        recyclerView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                onReloadData((true));
            }
        });
    }

    @Override
    protected void onChildViewCreated() {
        onChildViewCreatedPre();
        showBackImgLeft(getBackImgLeft());
        setTitleTxt(R.string.txt_continue_pay_manage);
        SZIRecyclerViewUtil.setVerticalLinearLayoutManager(this, recyclerView, android.R.color.transparent, SZUtil.dip2px(10));
        onReloadData(false);
    }

    @Override
    protected void onReloadData(final boolean isRefresh) {
        if(!isRefresh){
            showBarTransparentStyle();
        }
        request(SZRetrofitManager.getInstance().getSZRequest().getActivelyPaymentAgreement(UserManager.getInstance().getId()), new SZCommonResponseListener() {
            @Override
            public void onResponseSuccess(String data) {
                if(!isRefresh){
                    isShowContent(true);
                }else{
                    recyclerView.setRefreshing(false);
                }
                continuePayInfoList = new Gson().fromJson(data,new TypeToken<List<ActivelyPaymentInfo>>(){}.getType());
                continuePayManageAdp = new ContinuePayManageAdp(getActivity(),continuePayInfoList);
                continuePayManageAdp.setOnItemChildClickListener(new OnItemChildClickListener() {
                    @Override
                    public void onItemChildClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, final int position) {
                        if (view.getId() == R.id.txtCancel) {
                            showWarningAlert("解约续费", "解约后将不会自动续费并获取会员权益，确定要解约自动续费吗？", new SweetAlertDialog.OnSweetClickListener() {
                                @Override
                                public void onClick(SweetAlertDialog sweetAlertDialog) {
                                    sweetAlertDialog.dismissWithAnimation();
                                    removePayment(continuePayInfoList.get(position - 2));
                                }
                            });
                        }
                    }
                });
                recyclerView.setIAdapter(continuePayManageAdp);
            }

            @Override
            public void onResponseError(int errorCode, String errorMsg) {
                if(!isRefresh){
                    showErrorWithMsg(errorCode,errorMsg);
                }else{
                    recyclerView.setRefreshing(false);
                }
            }
        });
    }
    private void removePayment(final ActivelyPaymentInfo paymentInfo){
        showBarTransparentStyle();
        request(SZRetrofitManager.getInstance().getSZRequest().removePaymentAgreement(paymentInfo.getAppaId()), new SZCommonResponseListener() {
            @Override
            public void onResponseSuccess(String data) {
                closeBar();
                SZToast.success("取消成功！");
                continuePayInfoList.remove(paymentInfo);
                continuePayManageAdp.notifyDataSetChanged();
            }

            @Override
            public void onResponseError(int errorCode, String errorMsg) {
                closeBar();
            }
        });
    }

}
